package ee.serge.client;

import java.util.ArrayList;
import java.util.List;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.event.shared.SimpleEventBus;

import ee.serge.client.content.PageNotFoundContent;
import ee.serge.service.AdminModule;
import ee.serge.service.MainModule;
import ee.serge.service.Module;
import ee.serge.shared.Loan;
import ee.serge.util.Content;
import ee.serge.util.Loader;
import ee.serge.util.URLData;

public class Move implements HasValueChangeHandlers<Content> {

   private static Move move;
   
   public static Move get() {
      if (move == null) move = new Move();
      return move;
   }

   private List<Module> moduleList;
   private URLData currentURLData;
   private URLData prevURLData;
   private EventBus bus;
   private RootPanel mainContent;
   
   private Move() {
      mainContent = RootPanel.get("main");
      History.addValueChangeHandler(new ValueChangeHandler<String>() {
         @Override
         public void onValueChange(ValueChangeEvent<String> event) {
            changeContent(event.getValue());
         }
      });
      bus = new SimpleEventBus();
      moduleList = new ArrayList<Module>();
      moduleList.add(new MainModule());
      moduleList.add(new AdminModule());
   }
   
   public void refresh() {
      goToContent(currentURLData);
   }
   
   public void goToURLData(URLData data) {
      AlertBox.hide();
      History.newItem(data.getHref());
   }
   
   public void goToContent(URLData data) {
      AlertBox.hide();
      changeContent(data.getHref());
   }
   
   public URLData getCurrentURLData() {
      return currentURLData;
   }
   
   public URLData getPreviousURLData() {
      return prevURLData;
   }
   
   public void goBack() {
      if (prevURLData != null) {
         goToURLData(prevURLData);
      }
   }
   
   private void changeContent(String href) {
      URLData data = new URLData(href);
      mainContent.clear();
      try {
         prevURLData = currentURLData;
         currentURLData = data;
         for (Module module : moduleList) {
            module.handleContentChange(data.getName());
         }
      } catch (Exception e) {
         GWT.log("Invalid panel name. " + e);
         PageNotFoundContent content = new PageNotFoundContent();
         mainContent.add(content);
         ValueChangeEvent.fire(this, content);
         return;
      }
   }
   
   public void setContent(Content content) {
      mainContent.clear();
      mainContent.add(content);
      ValueChangeEvent.fire(this, content);
   }

   @Override
   public void fireEvent(GwtEvent<?> event) {
      bus.fireEvent(event);
   }

   @Override
   public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Content> handler) {
      return bus.addHandler(ValueChangeEvent.getType(), handler);
   }
   
}
